import unittest

def get(word,i):
	'''get word[i] if it exists, '' otherways'''
	try: return word[i]
	except IndexError: return ''

def list2string(items):
	'''join a list of words in a whole string with all words separated by newline ('\\n') '''
	assert(iter(items))
	return "\n".join(map(str,items))

def verticalize(*words):
	'''take a list of words and return a string that contains them vertically, ordered by length'''
	words = sorted(words, key=len, reverse=True)
	columns = ["".join(list(map(lambda x:get(x,i), words))) 
	           for i in range(len(words[0]))]
	return list2string(columns)

class Test(unittest.TestCase):
	def test_get(self):
		self.assertEqual(get([1],0), 1)		#good
		self.assertEqual(get(['a'],-1), 'a')	#good
		self.assertEqual(get([],0), '')		#none
		self.assertEqual(get(['a'],2), '')	#bad
	def test_list2s(self):
		self.assertEqual(list2string([1,2]), "1\n2")		#good
		self.assertEqual(list2string("ab"), "a\nb")		#good
		self.assertEqual(list2string(["foo",2]), "foo\n2")	#good
		self.assertEqual(list2string([]), "")			#none
		self.assertRaises(TypeError, list2string, 1)	#bad
	def test_vert(self):
		s1 = ["casa", "python", "mela", "a"]
		sr1 = "pcma\nyae\ntsl\nhaa\no\nn"
		self.assertEqual(verticalize(*s1), sr1)
		self.assertEqual(verticalize("a", "a", "a"), "aaa")

	
print(verticalize("casa", "python", "mela", "a"))
print(verticalize("a", "a", "a"))

unittest.main()


import itertools as it

def verticalize(*strings):
	'''from riko'''
	return (''.join(col)
            for col in it.izip_longest(
                *sorted(strings, key=len, reverse=True),
                fillvalue=''))
